/*
 * @Description: 
 * @Autor: 商晓彬
 * @Date: 2021-09-09 11:12:57
 * @LastEditors: 商晓彬
 * @LastEditTime: 2022-01-04 14:34:23
 */
import { LazyLoadEvent } from 'primeng/api';
import { Component, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/components/table/table';
import { Paginator } from 'primeng/components/paginator/paginator';
import { MessageService } from '@abp/message/message.service';
import { PrintMatingLabelServiceProxy, CommonServiceProxy, PrintLabelEditDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';
import { GoPrintComponent } from './go-print/go-print.component';
import { TreeNode } from 'primeng/api';


@Component({
    selector: 'app-print',
    templateUrl: './print.component.html',
    styleUrls: ['./print.component.less']
})
export class PrintComponent extends AppComponentBase {
    @ViewChild('GoPrintComponent') GoPrintComponent: GoPrintComponent;
    model = undefined;
    senTime = undefined;
    xj = undefined;
    bt = undefined;
    lotNo = undefined;
    lotName = undefined;
    modelOptions = [];
    senTimeOptions = [];
    xjOptions = [];
    btOptions = [];
    form = [];
    files1 = [];
    files2 = [];
    selectedTreeTable: any = [];
    selectedFiles1: any;
    constructor(
        injector: Injector,
        private _messageService: MessageService,
        private _printMatingLabelServiceProxy: PrintMatingLabelServiceProxy,
        private _commonServiceProxy: CommonServiceProxy,
    ) {
        super(injector);
    }
    nodeSelect(event) {
        this.selectedFiles1 = event.node;
        if (event.node.isRelation == 1) {
            this._printMatingLabelServiceProxy.getProductionPrintTechquine(
                event.node.id
            )
                .pipe(finalize(() => { }))
                .subscribe(result => {
                    let rowInfo = this.getTreeRecursion(this.files2, this.selectedFiles1.id);
                    rowInfo.children = result;
                });
        }
        if (event.node.nodeLevel == 8 || event.node.isRelation == 1) {
            this._printMatingLabelServiceProxy.displayPrintMatingNodeDetails(
                event.node.id
            )
                .pipe(finalize(() => { }))
                .subscribe(result => {
                    this.files1 = result;
                });
        }
    }
     // 递归循环树级结构 获取对象 通过物料ID 递归
     getTreeRecursion(data, current) {
        var result = null;
        if (!data) {
            // return; 中断执行
            return;
        }
        for (var i in data) {
            if (result != null) {
                break;
            }
            var item = data[i];
            // 通过id找到树结构相应节点 找到则跳出
            if (item.id == current) {
                result = item;
                break;
            } else if (item.children && item.children.length > 0) {
                result = this.getTreeRecursion(item.children, current);
            }
        }
        return result;
    }
    getTree() {
        this._printMatingLabelServiceProxy.getPrintProductionDesigenTree(
            this.model,
            this.senTime
        )
            .pipe(finalize(() => { }))
            .subscribe(result => {
                this.files2 = [];
                this.files2.push(result);
                this.expandAllTree();
            });
    }
    expandAllTree() {
        this.files2.forEach((node) => {
            this.expandRecursive(node, true);
        });
    }
    private expandRecursive(node: TreeNode, isExpand: boolean) {
        node.expanded = isExpand;
        if (node.children) {
            node.children.forEach((childNode) => {
                this.expandRecursive(childNode, isExpand);
            });
        }
    }
    backTOPrint() {

    }
    onBtChange(event) {
        //部套
        this._commonServiceProxy.getBopBt(
            this.model,
            this.senTime,
            undefined,
            this.xj
        ).pipe(finalize(() => { })).subscribe(result => {
            this.btOptions = result;
        })
    }
    onXjChange(event) {
        //芯级
        this._commonServiceProxy.getBopXj(
            this.model,
            this.senTime,
            undefined,
            undefined
        ).pipe(finalize(() => { })).subscribe(result => {
            this.xjOptions = result;
        })
    }
    print() {
        // this.printFlag = true;
        if (this.selectedTreeTable.length == 0) {
            this._messageService.info('请选择需要打印的数据');
            return;
        } else {
            this.form = this.selectedTreeTable;
           // for (let i = 0; i < this.form.length; i++) {
                //let item = this.form[i].data;
                // if (item.nodeLevel == 8) {
                //     this.form.splice(i, 1);
                // }
           // }
            this.GoPrintComponent.show();
        }

    }
    // getTreedata() {
    //     let xjName = undefined;
    //     let btName = undefined;
    //     let modelName = undefined;
    //     let sendTimeName = undefined;
    //     if (this.xj != undefined) {
    //         for (const item of this.xjOptions) {
    //             if (item.value == this.xj) {
    //                 xjName = item.label;
    //             }
    //         }
    //     }
    //     if (this.bt != undefined) {
    //         for (const item of this.btOptions) {
    //             if (item.value == this.bt) {
    //                 btName = item.label;
    //             }
    //         }
    //     }
    //     if (this.model != undefined) {
    //         for (const item of this.modelOptions) {
    //             if (item.value == this.model) {
    //                 modelName = item.label;
    //             }
    //         }
    //     }
    //     if (this.senTime != undefined) {
    //         for (const item of this.senTimeOptions) {
    //             if (item.value == this.senTime) {
    //                 sendTimeName = item.label;
    //             }
    //         }
    //     }
    //     let input = new PrintLabelEditDto();
    //     input.xjName = xjName;
    //     input.btId = this.bt;
    //     input.btName = btName;
    //     input.gcCode = this.lotNo;
    //     input.gcName = this.lotName;
    //     input.module = modelName;
    //     input.moduleId = this.model;
    //     input.sendTime = sendTimeName;
    //     input.sendTimeId = this.senTime;
    //     this._printMatingLabelServiceProxy.printMatingNodeDetails(input)
    //         .pipe(finalize(() => { }))
    //         .subscribe(result => {
    //             this.expandAll(result);
    //             this.files1 = result;
    //         });
    // }
    // expandAll(rs) {
    //     for (const node of rs) {
    //         this.TreeExpandRecursive(node, true);
    //     }
    // }
    // private TreeExpandRecursive(node: TreeNode, isExpand: boolean) {
    //     node.expanded = isExpand;
    //     if (node.children) {
    //         node.children.forEach((childNode) => {
    //             this.TreeExpandRecursive(childNode, isExpand);
    //         });
    //     }
    // }
    reset() {
        this.model = undefined;
        this.senTime = undefined;
        this.xj = undefined;
        this.bt = undefined;
        this.lotNo = undefined;
        this.lotName = undefined;
        this.senTimeOptions = [];
        this.xjOptions = [];
        this.btOptions = [];
    }
    ngOnInit() {
        this.getTree();
        //型号
        this._commonServiceProxy.getModelList(
        )
            .pipe(finalize(() => { })).subscribe(result => {
                this.modelOptions = result;
            })
    }
    onChange(event) {
         //发次
        this._commonServiceProxy.getLotNoList(
             this.model
            )
                .pipe(finalize(() => { })).subscribe(result => {
                    this.senTimeOptions = result;
                })
    }
}
